<!---->
<template>
  <div class="login">
    <div class="wrap">
      <div class="lf">
        <img src="../assets/user.png" width="100%" />
      </div>
      <div class="rf">
          <div class="title">登录</div>
          <div class="lg">
              <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item
                    prop="email"
                    label="邮箱"
                >
                    <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
                </el-form-item>
                </el-form>
                
          </div>
          <div class="check">
              <el-checkbox v-model="checked">记住密码</el-checkbox>
          </div>
          <div class="button">
              <el-button type="primary" @click="login">登录</el-button>
          </div>
          <div class="btm">
              <div @click="goReg(1)">注册账号</div>
              <div>|</div>
              <div @click="goReg(0)">忘记密码</div>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import<组件名字> from '<组件路径>';

export default {
  //import引入的组件需要注入对象中才能用
  components: {},
  data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
    return {
        checked: false,
        ruleForm: {
            email:'',
            password:'',
        },
        rules: {
            email:[{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }],
          password: [
            { required: true,validator: validatePass, trigger: 'blur' }
          ],
        }
      };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      goReg(i){
        if(i==1){
          this.$router.push('/reg?type=1')
        }else{
          this.$router.push('/reg?type=0')
        }
        
      },
      login(){
        this.API.login(this.ruleForm)
        .then(res=>{
          if(res.message.code == 200){
            sessionStorage.setItem("user",JSON.stringify(res.data))
            this.$store.dispatch('setUser',res.data)
            this.$router.push('/home')
          }else{
            this.$message.error(res.message.msg);
          }
          console.log('登录成功',res)
        })
      }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期-挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期-创建之前
  beforeMount() {}, //生命周期-挂载之前
  beforeUpdate() {}, //生命周期-更新之前
  updated() {}, //生命周期-更新之后
  beforeDestroy() {}, //生命周期-销毁之前
  destroyed() {}, //生命周期-销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个会输会触发
};
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
.wrap {
    display: flex;
    padding: 10px;
    color: #fff;
    position: absolute;
    width:800px;
    top: 50%;
    left: 50%;
    border-radius: 5px;
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: rgba(58, 98, 215, 100);
  .lf{
      flex:1;
  }
  .rf{
      flex: 1;
      display: flex;
      flex-direction: column;
      border:1px solid #ccc;
      background-color: rgba(255, 255, 255, 100);
      border-radius: 10px;
      .title{
          color:#000;
          text-align: center;
          padding:30px 0;
          font-size: 24px;
          font-weight: bold;
      }
      .lg{
          display: flex;
          justify-content: center;
      }
      .check{
              padding:0 80px;
          }
        .button{
            margin-top:30px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .btm{
            display: flex;
            justify-content: space-between;
            color:#000;
            padding: 20px 60px;
            :nth-child(odd){
                color:rgba(58, 98, 215, 100);
                cursor: pointer;
            }
        }
  }
}
.el-form{
    margin-right:50px
}
.el-button{
    width:280px;
}
</style>